//
//  JSBridgeKVDB.swift
//  ToBeBigFE
//
//  Created by mingo on 2020/2/10.
//  Copyright © 2020 com.example. All rights reserved.
//

import Foundation

class JSBridgeKVDB : BridgeModuelBase {
    
    weak var viewController: WebViewController?
    
    var userDefaults = UserDefaults(suiteName: "KVDB")!
    
    init(viewController: WebViewController) {
        self.viewController = viewController
    }
    
    override func callFunc(_ funcName: String, callbackId: String, arg: [String : Any?]) {
        let key = arg["key"] as! String
        switch funcName {
        case "getInt":
            callback(callbackId: callbackId, value: getInt(key: key))
        case "setInt":
            setInt(key: key, value: arg["value"] as? Int ?? 0)
        case "getBool":
            callback(callbackId: callbackId, value: getBool(key: key))
        case "setBool":
            setBool(key: key, value: arg["value"] as? Bool ?? false)
        case "getString":
            callback(callbackId: callbackId, value: getString(key: key))
        case "setString":
            setString(key: key, value: arg["value"] as? String)
        default: break
        }
    }
    
    func getInt(key: String) -> Int {
        return userDefaults.integer(forKey: key)
    }
    
    func setInt(key: String, value: Int) {
        userDefaults.set(value, forKey: key)
    }
    
    func getBool(key: String) -> Bool {
        return userDefaults.bool(forKey: key)
    }
    
    func setBool(key: String, value: Bool) {
        userDefaults.set(value, forKey: key)
    }
    
    func getString(key: String) -> String? {
        return userDefaults.string(forKey: key)
    }
    
    func setString(key: String, value: String?) {
        userDefaults.set(value, forKey: key)
    }
}
